package org.cuatrovientos.empirefleet;

import java.util.Scanner;
import java.util.Vector;

/**
 * Represents an imperial fleet composed
 * of many space_ships
 * @author Axel Brinck
 * @greets Pello Altadill
 */
public class Fleet extends Vector<SpaceShip> {
	String commanderName;
	
	/**
	 * creates fleet
	 * three steps: define commander name, create fleet, show fleet
	 */
	public void createFleet () {
		Scanner reader = new Scanner(System.in);
		String shipTie = "";
		String pilotName = "";
		
		requestCommanderName(reader);
		
		// We start a loop to define the fleet
		// In each loop commander chooses one kind of ship to be added
		// to his fleet
		do {
			System.out.println("What kind of ship do you want?");
			System.out.println("1.TF \n 2.TA \n 3.TB \n 4.Exit\n");
			shipTie = reader.nextLine();
			pilotName = requestPilotName();
			selectShipTie(shipTie, pilotName);
		} while(!shipTie.equals("4"));
		
		showFleetInfo();
		reader.close();
	}
	
	/**
	 * Request commander name from user input
	 * @param reader
	 */
	private void requestCommanderName(Scanner reader) {
		do {
			System.out.println("Identify yourself");
			commanderName = reader.nextLine();
		} while (commanderName.isEmpty());
	}
	
	/**
	 * Selects ship tie and its pilot name.
	 * @param shipTie
	 * @param pilotName
	 */
	private void selectShipTie(String shipTie, String pilotName) {
		switch (shipTie) {
			case "1": // a tie fighter
					this.add(new TieFighter(pilotName));
					break;
			case "2":
					this.add(new TieAdvanced(pilotName));
					break;
			case "3":
					this.add(new TieBomber(pilotName));
					break;
			case "4":
					System.out.println("Ended.");
					break;
			default:
					System.out.println("Option incorrect");
		}
	}

	/**
	 * Show complete fleet info
	 */
	private void showFleetInfo() {
		for (SpaceShip ship : this) {
			System.out.println("Type: " + ship.getType() + "," + ship.toString());
		}
	}
	
	/**
	 * Request pilot name from user input
	 * @return
	 */
	private String requestPilotName() {
		Scanner reader = new Scanner(System.in);
		String pilotName;
		System.out.println("Name of the pilot: "); 
		pilotName = reader.nextLine();
		reader.close();
		return pilotName;
	}
}
